using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using LS.UnityExtensions;

namespace LS.GameKit
{
    [System.Serializable]
    [CreateAssetMenu(menuName = "Lancer Studio/Enum Matrix Config",fileName = "EnumMatrixConfig", order = 50)]
    public sealed class EnumMatrixConfig : PreloadScriptableObjectSingleton<EnumMatrixConfig>
    {
        [SerializeField]
        private List<EnumContainer> m_Containers = new List<EnumContainer>();

        public List<EnumContainer> Containers => m_Containers;

        public EnumContainer this[int index]
        {
            get
            {
                if(index >= 0 && index < m_Containers.Count)
                    return m_Containers[index];
                else 
                    return null;
            }
        }
        
        public EnumContainer this[string key]
        {
            get
            {
                return m_Containers.First(c=>c.name == key);
            }
        }
        
        public int? GetValueByIndex(string typeName, int index)
        {
            return this[typeName]?[index]?.value;
        }
        
        public int? GetValue(string typeName, string enumName)
        {
            return Containers.First(c => c.name == typeName)?.GetValue(name);
        }
        
        public string GetName(string typeName, int enumValue)
        {
            return Containers.First(c => c.name == typeName)?.GetName(enumValue);
        }
        
        public EnumContainer GetContainer(string typeName)
        {
            return Containers.First(c => c.name == typeName);
        }

        public void AddContainer(EnumContainer container)
        {
            if (m_Containers.First(c => c.name == container.name) != null)
            {
                Debug.LogError("Container already exists");
                return;
            }
            m_Containers.Add(container);
        }
        
        public void AddContainer(string typeName)
        {
            if (m_Containers.First(c => c.name == typeName) != null)
            {
                Debug.LogError("Container already exists");
                return;
            }

            EnumContainer container = new EnumContainer() { name = typeName };
            AddContainer(container);
        }
        
        public void RemoveContainer(string typeName)
        {
            int index = m_Containers.FindIndex(c => c.name == typeName);
            if (index >= 0)
            {
                m_Containers.RemoveAt(index);
            }
        }

        public bool HasContainer(string typeName)
        {
            return GetContainer(typeName) != null;
        }
        
        public bool Contains(string typeName, string enumName)
        {
            return GetContainer(typeName)?.Contains(enumName) ?? false;
        }
        
        public bool Contains(string typeName, int enumValue)
        {
            return GetContainer(typeName)?.Contains(enumValue) ?? false;
        }
    }
}
